package com.unina.dropplayer.download;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.ProgressListener;
import com.dropbox.client2.android.AndroidAuthSession;
import com.example.dropplayer.R;
import com.unina.dropplayer.common.DropPlayerConstants;
import com.unina.dropplayer.common.GetFileListTask;
import com.unina.dropplayer.common.ProgressListenerImpl;
import com.unina.dropplayer.utilities.DropPlayerSessionBuilder;

public class DownloadActivity extends Activity {

	private DropboxAPI<AndroidAuthSession> mDBApi;
	private Toast toast;
	private ListView listView;
	private ListAdapter adapter;
	private ProgressDialog progressDialog;
	private DownloadTask downloadTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		AndroidAuthSession session = DropPlayerSessionBuilder
				.buildSession(this);
		mDBApi = new DropboxAPI<AndroidAuthSession>(session);

		setContentView(R.layout.activity_download);

		this.initDownloadActivity();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	private void initDownloadActivity() {

		listView = (ListView) findViewById(R.id.listView);
		GetFileListTask getFileListTask = new GetFileListTask(this, mDBApi,
				DropPlayerConstants.DROP_PLAYER_ROOT_FOLDER, listView, adapter);
		getFileListTask.execute();
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				// TODO Auto-generated method stub
				downloadFile(v);
			}

		});
		
		progressDialog = new ProgressDialog(DownloadActivity.this);
		progressDialog.setMessage(DropPlayerConstants.MESSAGE_DOWNLOADING);
		progressDialog.setIndeterminate(true);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		progressDialog.setCancelable(true);
		
		progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
		    @Override
		    public void onCancel(DialogInterface dialog) {
		        downloadTask.cancel(true);
		    }
		});

	}

	private void downloadFile(View v) {

		TextView txt = (TextView) v;
		ProgressListener progressListener = new ProgressListenerImpl(progressDialog);
		downloadTask = new DownloadTask(this, mDBApi,
				DropPlayerConstants.DROP_PLAYER_ROOT_FOLDER + txt.getText(),
				toast, progressListener, progressDialog);
		
		downloadTask.execute(DropPlayerConstants.DOWNLOAD_DESTINATION_FOLDER,txt.getText().toString());

	}
}
